// pages/registration/registration.js
let that,
  api = new(require('../../utils/api.js')),
  util = new(require('../../utils/util.js')),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    kindname:'',
    classname:'',
    babyname:'',
    classarr:[
      {
        id:1,
        name:'大一班',
      },
      {
        id:2,
        name:'大二班',
      },
      {
        id:3,
        name:'大三班',
      },
      {
        id:4,
        name:'大四班',
      },
      {
        id:5,
        name:'中一班',
      },
      {
        id:6,
        name:'中二班',
      },
      {
        id:7,
        name:'中三班',
      },
      {
        id:8,
        name:'中四班',
      },
      {
        id:9,
        name:'小一班',
      },
      {
        id:10,
        name:'小二班',
      },
      {
        id:11,
        name:'小三班',
      },
      {
        id:12,
        name:'小四班',
      },
    ],
    classnum:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    if (app.logininfo) {
      that.getdata()
    } else {
      util.setwatcher(app,that.afterlogin)
    }
    
  },
  afterlogin: {
    token: res => {
      that.getdata();
    }
  },
  getdata(){
    api.getKindergarten(res=>{
      if(res.data.issuccess==1){
        if(res.data.data){
          let {kindname,classname,babyname}=res.data.data
           that.setData({kindname,classname,babyname})
        }
      }
    })
  },
  inputtap(e){
    let {prop}=e.target.dataset,{value}=e.detail
    that.setData({[prop]:value})
  },
  pickchange(e) {
    console.log(e,'eee');
    let {classarr}=that.data
    that.setData({ classname:classarr[e.detail.value].name,classnum:classarr[e.detail.value].id})
  },
  register(){
    let {kindname,classname,babyname,KindergartenData,classnum} = that.data,err=''
    if(babyname=='') err='请输入宝宝姓名'
    if(classnum<=0)err="请选择班级名称"
    if(classname=='') err='请选择班级名称'
    if(kindname=='') err='请输入幼儿园名称'
    if(err.length>0){
      wx.showToast({
        title: err,
        icon:'none'
      })
      return
    }
    wx.showLoading({
      title: '登记中...',
    })
    api.aoeKindergarten({kindname,classname,babyname,classnum},res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        wx.showModal({
          title: '提示',
          content:"登记成功",
          showCancel:!1,
          success(res){
            if(res.confirm){
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "班级登记",
      path: `pages/registration/registration`,
    }
  }
})